package com.svnservices.hannibal.dao;

import com.svnservices.domaininterface.User;
import com.svnservices.hannibal.activerecord.UserAR;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; 
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import hannibal.dao.db.spring.Dao;

import java.sql.ResultSet;
import java.sql.SQLException;

@javax.annotation.Generated
(
	value="hannibal.generator.Generator",
	comments="",
	date = ""
)
   
public class UserDao
	extends Dao
{
	protected UserDao(javax.sql.DataSource _dataSource)
	{
		super(_dataSource);
	}

	protected ParameterizedRowMapper<UserAR> mapper = new ParameterizedRowMapper<UserAR>()
	{
		public UserAR mapRow(ResultSet _resultSet, int _rowNum)
				throws SQLException
		{
			UserAR user = new UserAR();

			user.setId(hannibal.util.DatabaseUtil.getString(_resultSet, "id"));
			user.setName(hannibal.util.DatabaseUtil.getString(_resultSet, "name"));
			user.setPassword(hannibal.util.DatabaseUtil.getString(_resultSet, "password"));
			
			return user;
		}
	};

	public java.util.Collection<UserAR> collect(int _offset, int _limit, String _orderBy)
	{
		String sql = "SELECT id AS id, name AS name, password AS password FROM User ORDER BY :orderBy LIMIT ? OFFSET ?";
		sql = sql.replaceAll(":orderBy", _orderBy);

		return query(sql, this.mapper, _limit, _offset);
	}

	public int insert(UserAR _user)
	{
		final String sql = "INSERT INTO User ( id, name, password) VALUES ( ?, ?, ?)";
		
		return update(sql, _user.getId(), _user.getName(), _user.getPassword());
	}


	public User get(final java.lang.String _id)
	{
		final String sql = "SELECT id AS id, name AS name, password AS password FROM User WHERE id = ?";

		return queryForObject(sql, this.mapper, _id);
	}

	public User get(UserAR _user)
	{
		return get(_user.getId());
	}

	public int update(UserAR _user)
	{
		final String sql = "UPDATE User SET name = ?, password = ? WHERE id = ?";

		return update(sql, _user.getName(), _user.getPassword(), _user.getId() );
	}

	public int del(final java.lang.String _id)
	{
		return delete(_id);
	}

	public int delete(final java.lang.String _id)
	{
		final String sql = "DELETE FROM User WHERE id = ?";

		return update(sql, _id);
	}

	public int del(UserAR _user)
	{
		return delete(_user);
	}

	public int delete(UserAR _user)
	{
		return delete(_user.getId());
	}


}